/*server.c*/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <pthread.h>
#define PORT			4321
#define BUFFER_SIZE		1024
#define MAX_QUE_CONN_NM	5
#define FILE_NAME_MAX_SIZE         512

void *net_thread(void *fd);

int main( )
{
	struct sockaddr_in server_sockaddr;
	int sin_size,recvbytes;
	int sockfd, client_fd;
	char buf[BUFFER_SIZE];
	
	/*建立socket连接*/
	if ((sockfd = socket(AF_INET,SOCK_STREAM,0))== -1)
	{
		perror("socket");
		exit(1);
	}
	printf("Socket id = %d\n",sockfd);
	
	/*设置sockaddr_in 结构体中相关参数*/
	server_sockaddr.sin_family = AF_INET;
	server_sockaddr.sin_port = htons(PORT);
	server_sockaddr.sin_addr.s_addr = INADDR_ANY;
	bzero(&(server_sockaddr.sin_zero), 8);
	
	int i = 1;/* 允许重复使用本地地址与套接字进行绑定 */
	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));	
	
	/*绑定函数bind()*/
	if (bind(sockfd, (struct sockaddr *)&server_sockaddr, sizeof(struct sockaddr)) == -1)
	{
		perror("bind");
		exit(1);
	}
	printf("Bind success!\n");
	
	/*调用listen()函数，创建未处理请求的队列*/
	if (listen(sockfd, MAX_QUE_CONN_NM) == -1)
	{
		perror("listen");
		exit(1);
	}
	printf("Listening....\n");
	
	
	 while(1)  
    {  

        struct sockaddr_in client_sockaddr;  
        socklen_t          length = sizeof(client_sockaddr);  
  
   
	if ((client_fd = accept(sockfd, (struct sockaddr *)&client_sockaddr, &length)) == -1)
	{
		perror("accept");
		exit(1);
	}
  
  	pthread_t pthread_id;
  	int ret = pthread_create(&pthread_id,NULL,net_thread,(void *)&client_fd);
  	if(-1==ret)
	{
		perror("pthread_create");
		close(client_fd);
		continue;
	}

          
    }  
  
    close(sockfd);  
  
    return 0;  
}

void *net_thread(void *fd)
{
	pthread_detach(pthread_self());
	int new_fd=*((int *)fd);
	while(1)
	{
		socklen_t  length = 0;
		char buffer[BUFFER_SIZE];  
		bzero(buffer, sizeof(buffer));  
		length = recv(new_fd, buffer, BUFFER_SIZE, 0);  
		if (length < 0)  
		{  
		    printf("Server Recieve Data Failed!\n");  
		    break;  
		}  
	  
		char file_name[FILE_NAME_MAX_SIZE + 1];  
		bzero(file_name, sizeof(file_name));  
		strncpy(file_name, buffer, strlen(buffer) > FILE_NAME_MAX_SIZE ? FILE_NAME_MAX_SIZE : strlen(buffer));  
	  
		FILE *fp = fopen(file_name, "r");  
		if (fp == NULL)  
		{  
		    printf("File:\t%s Not Found!\n", file_name);  
		}  
		else  
		{  
		    bzero(buffer, BUFFER_SIZE);  
		    int file_block_length = 0;  
		    while( (file_block_length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)  
		    {  
		        printf("file_block_length = %d\n", file_block_length);  
	  
		        // 发送buffer中的字符串到new_server_socket,实际上就是发送给客户端  
		        if (send(new_fd, buffer, file_block_length, 0) < 0)  
		        {  
		            printf("Send File:\t%s Failed!\n", file_name);  
		            break;  
		        }  
	  
		        bzero(buffer, sizeof(buffer));  
		    }  
		    fclose(fp);  
		    printf("File:\t%s Transfer Finished!\n", file_name);  
		    break;
		}  
	}  
        close(new_fd);
        return NULL;
}

